package com.yashi.appController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.yashi.entity.TbDepartment;
import com.yashi.entity.TbUser;
import com.yashi.service.DepartmentService;
import com.yashi.service.UserService;

@RestController
@RequestMapping("/appUser")
public class AppUserController {
	
	@Resource
	private WebSocket webSocket;
	
	@Resource
	private UserService userService;
	
	@Resource
	private DepartmentService departmentService;
	
	/**
	 * 登录验证
	 * @param user
	 * @param session
	 * @return
	 */
	@RequestMapping("/login")
	public Map<String,Object> userLogin(TbUser user,HttpSession session,HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			
			TbUser user1 = userService.selectByUser(user);
			session.setAttribute("user", user1);
			session.setMaxInactiveInterval(0);
			map.put("status", "1");
			map.put("message","登录成功");
			map.put("data", user1);
			return map;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			request.setAttribute("error", e.getMessage());
			map.put("status","0");
			map.put("message",e.getMessage());
			return map;
		}
		
	}
	
	/**
	 * 根据Id查询用户
	 * @param userId
	 * @return
	 */
	@RequestMapping("/findUserById")
	public Map<String,Object> findUserById(String userId){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			TbUser user = userService.findUserById(userId);
			map.put("status", "1");
			map.put("message","查询个人信息成功");
			map.put("data", user);
			return map;
		} catch (Exception e) {
			map.put("status","0");
			map.put("message","查询个人信息失败");
			return map;
		}
	}
	
	/**
	 * 查询所有用户
	 * @param user
	 * @param request
	 * @return
	 */
	@RequestMapping("/findUserList")
	public Map<String,Object> findUserlist(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<TbUser> findUserList = userService.findUserList1();
			request.setAttribute("findUserList", findUserList);
			map.put("status", "1");
			map.put("message","查询成功");
			map.put("data", findUserList);
			return map;
		} catch (Exception e) {
			map.put("status","0");
			map.put("message","查询失败");
			return map;
		}
	}
	
	
	/**
	 * 模糊查询
	 * @param userName
	 * @param request
	 * @return
	 */
	@RequestMapping("/selectUserByName")
	public Map<String,Object> selectUserByName( TbUser user,HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<TbUser> selectUserList = userService.selectUserByName1(user);
			request.setAttribute("findUserList", selectUserList);
			map.put("status", "1");
			map.put("message","查询成功");
			map.put("data", selectUserList);
			return map;
		} catch (Exception e) {
			map.put("status","0");
			map.put("message","查询失败");
			return map;
		}
	}
	
	/**
	 * 更新用户
	 * @param user
	 * @param session
	 * @return
	 */
	@RequestMapping("/updateUser")
	public Map<String,Object> updateUser( TbUser user,HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			userService.updateUser(user,session);
			map.put("status", "1");
			map.put("message","更新成功");
			return map;
		} catch (Exception e) {
			map.put("status","0");
			map.put("message","更新失败");
			return map;
		}
	}
	
	/**
	 * 为更新查找用户
	 * @param userId
	 * @param request
	 * @return
	 */
	@RequestMapping("/selectUserById")
	public String selectUserById( String userId,HttpServletRequest request){
		try {
			TbUser user = userService.selectUserById(userId);
			request.setAttribute("user1", user);
			PageInfo<TbDepartment> page= departmentService.findDeptList1(1);
			request.setAttribute("findDeptList", page.getList());
			return "toUpdateUser";
		} catch (Exception e) {
			return e.getMessage();
		}
	}
	
	/**
	 * 修改密码
	 * @param userId  用户Id
	 * @param oldPassword   旧密码
	 * @param newPassword   新密码
	 * @param confirmPass   确认密码
	 * @return
	 */
	@RequestMapping("/updatePass")
	public Map<String, Object> updatePass(String userId, String oldPassword,  String newPassword, String confirmPass){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			userService.updatePass(userId,oldPassword,newPassword,confirmPass);
			map.put("status", "1");
			map.put("message","密码修改成功");
			return map;
		} catch (Exception e) {
			map.put("status", "0");
			map.put("message","密码修改失败");
			return map;
		}
	}

	/**
	 * 注销
	 * @param session
	 * @return
	 */
	@RequestMapping("/logout")
	public Map<String, Object> logout(HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			session.removeAttribute("user");
			webSocket.onClose();
			map.put("status", "1");
			map.put("message","退出成功");
			return map;
		} catch (Exception e) {
			map.put("status", "0");
			map.put("message","退出失败");
			return map;
		}
		
		
	}
	
	
	/**
	 * 修改头像
	 * @param userId 用户Id
	 * @param file   文件
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping("/modifyImg")
	public Map<String, Object> modifyImg(String userId,@RequestParam("file") MultipartFile file,
            HttpServletRequest request,HttpSession session){
			Map<String, Object> map = new HashMap<String, Object>();
			try {
				String url = userService.modifyImg(userId,file,request,session);
				map.put("status", "1");
				map.put("message","头像修改成功");
				map.put("data", url);
				return map;
			} catch (Exception e) {
				map.put("status", "0");
				map.put("message","头像修改失败");
				return map;
			}
	}
}
